package com.xian.im.modules.avchat;

import com.xian.feign.UserChannelClient;
import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.constant.CommandConstant;
import com.xian.im.common.constant.ThreadLocalConstant;
import com.xian.im.common.model.session.SessionUser;
import com.xian.im.common.properties.IErrorCodeProperties;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.utils.bussiness.XimResponseUtil;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.common.utils.sys.ThreadLocalUtil;
import com.xian.im.modules.command.CommandContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @program: xian-code
 * @description: messageConversion
 * @author: liru.xian
 * @create: 2021-01-21 16:31
 */
@Slf4j
@Component
public class IMCoreHandlerContext {

    @Autowired
    private IErrorCodeProperties errorCodeProperties;
    @Autowired
    private UserChannelClient userChannelClient;

    public static final String MARK = "*";

    /**
     * 队列解耦
     *
     * @param msg
     * @param ctxId
     */
    public void accept(MsgRequest msg, String ctxId) {
        SessionUser session = userChannelClient.session(ctxId);
        try {
            ThreadLocalUtil.set(ThreadLocalConstant.REQ, msg);
            // 业务处理
            CommandContext.isMatch(msg.getCmd()).exec(msg, ctxId);
        } catch (Exception e) {
            log.info("消息异常 {}", e.getMessage());
            // 如果发送人不存在 当前服务不进行错误处理

            int code = 1000;
            for (String s : errorCodeProperties.getErrors().keySet()) {
                String value = errorCodeProperties.getErrors().get(s);
                if (value.equals(e.getMessage())) {
                    code = Integer.valueOf(s);
                    if (1002 == code) {
                        return;
                    }
                }
            }
            MsgResponse error = XimResponseUtil.buildError(session, e.getMessage(), code);
            error.setCmd(CommandConstant.SYSTEM_CMD);
            if (CommandConstant.GROUP_CMD.equals(msg.getCmd())) {
                if (1018 == code) {
                    error.setCmdCode(CommandCodeConstant.SYSTEM_SENSITIVE_GROUP);
                } else {
                    error.setCmdCode(CommandCodeConstant.SYSTEM_GROUP);
                }
            } else if (CommandConstant.P2P_CMD.equals(msg.getCmd())) {
                if (1018 == code) {
                    error.setCmdCode(CommandCodeConstant.SYSTEM_SENSITIVE_USER);
                } else {
                    error.setCmdCode(CommandCodeConstant.SYSTEM_USER);
                }
            }
            build(error, e.getMessage());
            userChannelClient.pushChannelMessage(ctxId, error);
        } finally {
            ThreadLocalUtil.remove();
        }
    }

    public static String replace(Integer size) {
        StringBuilder sb = new StringBuilder();
        for (Integer i = 0; i < size; i++) {
            sb.append(MARK);
        }
        return sb.toString();
    }

    public static void build(MsgResponse response, String errorMessage) {
        IErrorCodeProperties errorCodeProperties = SpringContextHolder.getBean(IErrorCodeProperties.class);
        Map<String, String> errors = errorCodeProperties.getErrors();
        for (String s : errors.keySet()) {
            String message = errors.get(s);
            if (message.equals(errorMessage)) {
                response.setCode(Integer.valueOf(s));
                response.setMsg(message);
            }
        }
    }

}
